package com.pine.web.pineframework.controller;

import com.pine.common.beans.ResultVo;
import com.pine.common.beans.pineframework.request.SysRoleAssignMenuRequest;
import com.pine.common.beans.pineframework.request.SysRoleQueryRequest;
import com.pine.common.beans.pineframework.request.SysRoleSaveOrUpdateRequest;
import com.pine.common.beans.pineframework.request.SysRoleUpdateStatusRequest;
import com.pine.common.beans.pineframework.response.SysRoleQueryResponse;
import com.pine.common.beans.request.BatchIdRequest;
import com.pine.common.beans.request.IdRequest;
import com.pine.common.beans.request.PageRequest;
import com.pine.common.beans.response.DeleteResponse;
import com.pine.common.beans.response.PageResponse;
import com.pine.common.systemlog.annotation.SysLog;
import com.pine.manager.pineframework.trigger.SysRoleTriggerManager;
import com.pine.service.pineframework.SysRoleMenuService;
import com.pine.service.pineframework.SysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 系统角色前端控制器
 * </p>
 *
 * @author pine manage
 * @since 2024-08-14
 */
@Tag(name = "系统角色前端控制器")
@RestController
@RequestMapping("/sys/role")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    @Resource
    private SysRoleMenuService sysRoleMenuService;

    /**
     * 分页查询
     *
     * @param request 请求
     * @return 分页响应
     */
    @Operation(summary = "分页查询")
    @PostMapping("/pages")
    @SysLog("分页查询")
    public ResultVo<PageResponse<SysRoleQueryResponse>> listPages(@RequestBody @Valid PageRequest<SysRoleQueryRequest> request) {
        request.defaultPage();
        return ResultVo.success(sysRoleService.listPages(request));
    }

    /**
     * 查询信息（不分页）
     *
     * @param request 请求
     * @return PageResponse 响应
     */
    @Operation(summary = "查询信息（不分页）")
    @PostMapping("/list")
    @SysLog("查询信息（不分页）")
    public ResultVo<List<SysRoleQueryResponse>> list(@RequestBody @Valid SysRoleQueryRequest request) {
        return ResultVo.success(sysRoleService.list(request));
    }

    /**
     * 新增
     *
     * @param request 请求
     * @return 响应
     */
    @PostMapping(value = "/save")
    @Operation(summary = "新增信息")
    @SysLog("新增信息")
    public ResultVo<Boolean> save(@RequestBody @Valid SysRoleSaveOrUpdateRequest request) {
        Boolean triggerResult = SysRoleTriggerManager.saveOrUpdate(request);
        return ResultVo.success(triggerResult);
    }

    /**
     * 更新
     *
     * @param request 请求
     * @return 响应
     */
    @PostMapping(value = "/update")
    @Operation(summary = "更新信息")
    @SysLog("更新信息")
    public ResultVo<Boolean> update(@RequestBody @Valid SysRoleSaveOrUpdateRequest request) {
        Boolean triggerResult = SysRoleTriggerManager.saveOrUpdate(request);
        return ResultVo.success(triggerResult);
    }

    /**
     * 删除
     *
     * @param request 请求
     * @return 响应
     */
    @PostMapping(value = "/delete")
    @Operation(summary = "删除信息")
    @SysLog("删除信息")
    public ResultVo<DeleteResponse> delete(@RequestBody @Valid BatchIdRequest<Long> request) {
        return ResultVo.success(SysRoleTriggerManager.delete(request));
    }

    /**
     * 修改状态
     *
     * @param request 请求
     * @return 响应
     */
    @PostMapping(value = "/updateStatus")
    @Operation(summary = "修改状态")
    @SysLog("修改状态")
    public ResultVo<Boolean> updateStatus(@RequestBody @Valid SysRoleUpdateStatusRequest request) {
        Boolean triggerResult = SysRoleTriggerManager.updateStatus(request);
        return ResultVo.success(triggerResult);
    }

    /**
     * 分配菜单给角色（含按钮权限）
     *
     * @param request 请求
     * @return 响应
     */
    @PostMapping(value = "/assignMenu")
    @Operation(summary = "分配菜单给角色（含按钮权限）")
    @SysLog("分配菜单给角色（含按钮权限）")
    public ResultVo<Boolean> assignMenu(@RequestBody @Valid SysRoleAssignMenuRequest request) {
        return ResultVo.success(SysRoleTriggerManager.assignMenu(request));
    }

    /**
     * 获取角色对应的菜单ID集合
     *
     * @param request 请求-角色ID
     * @return 响应
     */
    @PostMapping(value = "/getMenuIdsByRoleId")
    @Operation(summary = "获取角色对应的菜单ID集合")
    @SysLog("获取角色对应的菜单ID集合")
    public ResultVo<List<Long>> getMenuIdsByRoleId(@RequestBody @Valid IdRequest<Long> request) {
        return ResultVo.success(sysRoleMenuService.getMenuIdsByRoleId(request));
    }
}
